package zj.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.interfaze.Callback;
import zj.com.model.GsTj;
import zj.com.model.GsTjOrg;
import zj.com.model.InspectItem;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/22.
 */
public class TjExpandableListViewAdapter extends BaseExpandableListAdapter {

    public Context context;
    public LayoutInflater lif;

    public List<GsTj> list;

    public TjExpandableListViewAdapter(Context context, List<GsTj> list) {
        this.context = context;
        lif = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getOrgsList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getOrgsList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = lif.inflate(
                    R.layout.header_t, null);
            groupHolder = new GroupHolder();
            groupHolder.name_tv = (TextView) convertView.findViewById(R.id.title_tv);
            groupHolder.count_tv = (TextView) convertView.findViewById(R.id.count_tv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        groupHolder.name_tv.setText(list.get(groupPosition).getName());
        groupHolder.count_tv.setText(list.get(groupPosition).getTotalCount()+"");
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ItemHolder itemHolder = null;
        if (convertView == null) {
            convertView = lif.inflate(
                    R.layout.item_t, null);
            itemHolder = new ItemHolder();
            itemHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            itemHolder.any_tv = (TextView) convertView.findViewById(R.id.any_tv);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }

      List<GsTjOrg> contents =   list.get(groupPosition).getOrgsList();
       final GsTjOrg item = contents.get(childPosition);
        if(contents != null)  {
            itemHolder.name_tv.setText(item.getOrgName());
            itemHolder.any_tv.setText(item.getAnalysisCount()+"");
        }
        return convertView;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupHolder {
        public TextView name_tv;
        public TextView count_tv;
    }

    class ItemHolder {
        public TextView name_tv;
        public TextView any_tv;
    }

    public void setDataChanage(List<GsTj> list){

        this.list = list;
        notifyDataSetChanged();

    }

    public Callback callback;
    public void setCallback(Callback call){
        this.callback = call;
    }


}



